<?php


/**
 * 角色组管理
 * Enter description here ...
 * @author Administrator
 *
 */
class RoleAction extends CommonAction{
	
	
	/**
	 * 权限组列表首页
	 * Enter description here ...
	 */
	public function index() {
		$role = D('Role');		
		
		$this->page($role,$roledata,$page);  //父类分页查询方法
		
		$this->assign("page",$page);
		$this->assign("roledata",$roledata);
		
		$this->display();
	}
	
	
	
	/**
	 * 删除单个用户角色组
	 * @see CommonAction::del()
	 */
	public function del() {
		$role = D('Role');
		$prefix = C('DB_PREFIX');  //数据表表前缀
		
		if(!empty($role)) {
			$id       = GetNum($_GET['id']);   //角色组主键ID
			if(isset($id)) {
				//删除角色组级联删除角色用户信息和角色功能信息
				$sql = "DELETE R,A,RU FROM `{$prefix}role` as R LEFT JOIN `{$prefix}access` as A ON R.id = A.role_id LEFT JOIN `{$prefix}role_user` as RU ON R.id = RU.role_id WHERE R.id = {$id}";
				if($role->execute($sql)){
					$this->success("删除成功");
				}else {
					$this->error("删除失败");
				}
			}else {
				$this->error('非法操作');
			}
		}
	}

	
	
	/**
	 * 删除多个用户角色组
	 * @see CommonAction::dels()
	 */
	public function dels()	{
		$role = D('Role');
		$prefix = C('DB_PREFIX');
		
		if(!empty($role)) {
			$ids       = getdotstring($_POST['ids'],'int');  //角色IDS
			//删除角色组级联删除角色用户信息和角色功能信息
			$sql = "DELETE R,A,RU FROM `{$prefix}role` as R LEFT JOIN `{$prefix}access` as A ON R.id = A.role_id LEFT JOIN `{$prefix}role_user` as RU ON R.id = RU.role_id WHERE R.id in({$id})";
			if ($role->execute($sql)){
				$this->success('已删除选定项');
			}else{
				$this->error($role->getError());
			}
		}
	}
	
	
	
	
		
	/**
	 * 新增用户角色组
	 * @see CommonAction::add()
	 */
	public function add(){
		$role = D('Role');  //角色Model
		$node = M('Node');  //节点Model
		
		$roledata = $role->select();  //所有角色权限组
		
		$where['status'] = 1;   //启用状态
		$where['level'] = 2;   //级别  ： 1级属于根应用（后台管理）  2级属于功能菜单（系统管理、订单管理等） 3级属于权限控制菜单（新增、删除等）
		$node_tmp = $node->where($where)->findAll();  //所有功能菜单
		
		foreach ($node_tmp as $val){
			$where = null;
			$where['status'] = 1;  //启用状态
			$where['level'] = 3; //权限控制菜单
			$where['pid'] = $val['id'];  //上级父ID是当前功能菜单
			
			$val['son'] = $node->where($where)->findAll();
			
			$nodedata[] = $val;
		}
		
		$this->assign("roledata",$roledata);
		$this->assign("nodedata",$nodedata);
		
		$this->display();
	}
	
	
	
	
	/**
	 * 确认保存权限角色组
	 * @see CommonAction::insert()
	 */
	public function insert(){
		$role = D('Role');    //角色Module
		
		$node = M('Node');    //节点Module
		$access = M('Access'); //角色节点访问Module
		
		
		$data = Init_GP(array('name','pid','status'));   //权限组基本信息
		$nodedata = Init_GP(array('node'));   //权限组角色控制
		
		$nodedata = $nodedata['node'];
		
		$data = $role->create($data,1);
		if(empty($data) || $data === false) {
			$this->error($role->getError());
		}
		
		$id = $role->add($data);   		//保存当前角色组基本数据对象
		if($id) { //保存成功
			//成功提示
			
			//插入角色分组权限
			if (!empty($nodedata)){
				$accessdata[] = array(
					'role_id'=>$id,  
					'node_id'=>1,
					'level'=>1
				);
				foreach ($nodedata as $node_id){
					$node_id = GetNum($node_id);  //节点ID
					
					$node_tmp = null;
					$node_tmp = $node->find($node_id);   //查询节点数据库信息
					
					$accessdata[] = array(
						'role_id'=>$id,
						'node_id'=>$node_tmp['id'],
						'level'=>$node_tmp['level']
					);
				}
				$access->addAll($accessdata);   //保存角色节点权限控制
				
			}
			
			
			$this->assign('jumpUrl','__URL__/index');
			$this->success("添加成功");
		}else {
			//失败提示
			$this->error($role->getError());
		}
	}
	
	
	
	
	
	
	/**
	 * 修改权限角色
	 * @see CommonAction::edit()
	 */
	public function edit(){
		$role = D('Role');
		$node = M('Node');
		$access = M('Access');
		
		//权限节点数据
		$where['status'] = 1;   //启用状态
		$where['level'] = 2;
		$node_tmp = $node->where($where)->findAll();   //权限节点（功能菜单，级别为2）
		
		foreach ($node_tmp as $val){  //获取功能菜单下属子功能菜单（权限控制，级别为3）
			$where = null;
			$where['status'] = 1;
			$where['level'] = 3;
			$where['pid'] = $val['id'];
			
			$val['son'] = $node->where($where)->findAll();
			
			$nodedata[] = $val;
		}
		
		
		$id     = GetNum($_GET['id']);  //权限组ID
		$vo	=	$role->find($id);
		
		$roledata = $role->where("id <> {$id}")->select();   //除去当前权限组所有权限组
		
		
		//权限数据
		$access_where['role_id'] = $id;   
		$access_tmp = $access->where($access_where)->findAll();   //当前角色组下属访问控制权限
		
		foreach ($access_tmp as $value){
			$accessdata[]= $value['node_id'];  //节点ID
		}
		
		$this->assign("roledata",$roledata);
		$this->assign('data',$vo);
		$this->assign("nodedata",$nodedata);
		$this->assign("accessdata",$accessdata);
		
		$this->display();
	}
	
	
	
  
    /**
	 * 修改权限角色确认
	 * @see CommonAction::update()
	 */
	public function update(){
		$role = D('Role');
		$node = M('Node');
		$access = M('Access');
		
		$dataarr = Init_GP(array('name','pid','status','id'));
		$nodedata = Init_GP(array('node'));
		$nodedata = $nodedata['node'];
		
		$data['pid'] = $dataarr['pid'];
		$data['status'] = $dataarr['status'];
		$data['name'] = $dataarr['name'];
		$where['id'] = $dataarr['id'];

		$data = $role->create($data,2);
		if(empty($data) || $data === false) {
			$this->error($role->getError());
		}
		
		$id = $role->where($where)->save($data);   //修改
		
		if($id !== false) { //保存成功
			//成功提示
			//更新分组权限
			if ($id === 0){
				//用户信息没有修改 但是权限修改的情况下
				$role_tmp = $role->where($where)->find();
				$id = GetNum($role_tmp['id']);
			}
			
			
			//清楚已有的权限
			$access_where['role_id'] = $id;
            $access->where($access_where)->delete();
            
            $accessdata[] = array(
					'role_id'=>$id,
					'node_id'=>1,
					'level'=>1
			);
			foreach ($nodedata as $node_id){
				$node_id = GetNum($node_id);
				
				$node_tmp = null;
				$node_tmp = $node->find($node_id);
				$access_tmp = array(
					'role_id'=>$id,
					'node_id'=>$node_tmp['id'],
					'level'=>$node_tmp['level']
				);
				
				$accessdata[] = $access_tmp;
			}
			
			$access->addAll($accessdata);
			
			$this->assign('jumpUrl','__URL__/index');
			$this->success("编辑成功");
		}else {
			//失败提示
			$error = $role->getError();
			if (empty($error)) {
				$this->error("编辑失败");
			}else {
				$this->error($error);
			}
		}	
		
	}
	
	
	
	
	
	
}
?>